package de.tuberlin.julia.SFD_Numbers;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import com.google.code.microlog4android.Logger;
import com.google.code.microlog4android.LoggerFactory;

import de.tlabs.ahmad.trigger_server.TriggerServer;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;

public class EndView extends Activity{
	
	private static final Logger logger = LoggerFactory.getLogger();
	
	Button end;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        	
		setContentView(R.layout.endview);	
        end = (Button) findViewById(R.id.Button01);
        
        end.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {          
            	
                // Datum
                TimeZone tz = TimeZone.getTimeZone("Europe/Berlin");
                Locale locale = Locale.GERMANY;
                final Calendar c = Calendar.getInstance(tz,locale);   
                
                SimpleDateFormat f= new SimpleDateFormat("dd.MM.yyyy hh:mm");
                f.setTimeZone(c.getTimeZone());
                Date date = c.getTime();

                logger.info("* " + f.format(date) + " end");
                
            	logger.info("Ende");
                
                try {
					TriggerServer.StopTriggerServer();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                finish();
            }

        });
	}

    protected void onDestroy(){
    	super.onDestroy();
    	
    	File sdcard = Environment.getExternalStorageDirectory();    	
    	
        // rename microlog.txt to VPx.log
    	File from = new File(sdcard,"microlog.txt");
    	File to = new File(sdcard,"VP" + this.getIntent().getStringExtra("vpStr").toString() + ".log");
    	from.renameTo(to); 
    	
    	try {
    		logger.resetLogger();
			logger.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }


}
